package com.c.ctgproject.ui.collect

import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.PartCanHandlerAdapter
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityPartHandlerListBinding
import com.c.ctgproject.entity.guang.BatteryCanHandlerEntity
import com.c.ctgproject.entity.guang.PartOutListEntity
import com.c.ctgproject.event.PositionsEvent
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import org.greenrobot.eventbus.Subscribe
import org.jetbrains.anko.startActivity

/**
 *作者:LiBW
 *创建日期: 2021/9/17
 *描述: 零部件拆解单 页面 新建拆解单 页面(零部件)
 */
class PartHandlerListActivity : BaseActivityV2(true) {
    private val mBinding by lazy { ActivityPartHandlerListBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private var search = ""
    private var page = 1
    private val mList = ArrayList<PartOutListEntity.DataBean.ListBean>()
    private val mViewModel:PartHandlerListModelView by lazy { ViewModelProvider(this)[PartHandlerListModelView::class.java] }
    private val mAdapter by lazy { PartCanHandlerAdapter(this, mList,mViewModel) }

    @Subscribe
    fun event(type: PositionsEvent) {
        val removeList = ArrayList<PartOutListEntity.DataBean.ListBean>()
        type.list.forEach { a ->
            mList.forEach { b ->
                if (a == b.partsId) {
                    removeList.add(b)
                }
            }
        }
        removeList.forEach {
            mList.remove(it)
        }

        mAdapter?.notifyDataSetChanged()
    }

    override fun event() {
        getData()
        mBinding.mRecyclerView.adapter = mAdapter
        mBinding.mRecyclerView.layoutManager = LinearLayoutManager(this)
        mViewModel.carIdList.observe(this, Observer {
            if(it.isEmpty()){
                mBinding.mConfirmBTN.text="拆解"
            }else{
                mBinding.mConfirmBTN.text="拆解（${it.size}）"
            }
        })
    }

    var searchMine = false
    override fun click() {
        mBinding.mTabLayout.handlerSelectReturnText {
            when (it) {

                "未拆解" -> {
                    mBinding.mBottomRL.visibility = View.VISIBLE
                    searchMine = false
                    mViewModel.changeSearchMine(false)
                    page = 1
                    getData()
                }
                "我的" -> {
                    mBinding.mBottomRL.visibility = View.GONE
                    mViewModel.changeSearchMine(true)
                    searchMine = true
                    page = 1
                    getData()
                }
            }
        }

        mBinding.mSearchET.handlerText {
            if (it.isEmpty() || it == "") {
                search = ""
                page = 1
                getData()
            }
        }
        mBinding.mSearchTV.setOnClickListener {
            search = mBinding.mSearchET.text.handlerNull()
            page = 1
            getData()
        }
        mBinding.mRefresh.setOnLoadMoreListener {
            page++
            getData()
        }

        mBinding.mRefresh.setOnRefreshListener {
            page = 1
            getData()
        }


        //去拆解
        mBinding.mConfirmBTN.setOnClickListener {
            val selectList = ArrayList<PartOutListEntity.DataBean.ListBean>()
            mList.forEach {
                if (it.isSelect) selectList.add(it)
            }
            if (selectList.isEmpty()) {
                showToast("请至少选择一个进行拆解")
            } else {
                //去拆解
                startActivity<PartHandlerActivity>(MyConstants.DATA to selectList)
            }
        }
    }

    private fun getData() {
        mViewModel.clear()
        if (searchMine) {
            //状态 1-拆解中 2-拆解完成 (根据状态查询 新建是1 我的拆解单列表是2)
            ApiMethods.apiSubscribe(ApiMethods.retrofit.myPartHandlerList(search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("我的零部件拆解单回调:$it")
                val data = getGson().fromJson(it, PartOutListEntity::class.java).data
                data?.list?.let { list ->
                    if (page == 1) mList.clear()
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mRecyclerView.handlerPosition(page)
                }
            })
        } else {
            ApiMethods.apiSubscribe(ApiMethods.retrofit.partOutListV3(search, page), MyObserverHandlerLoading(this, loading, mBinding.mRefresh) {
                MyLog.d("零部件拆解单回调:$it")
                val data = getGson().fromJson(it, PartOutListEntity::class.java).data
                data?.list?.let { list ->
                    if (page == 1) mList.clear()
                    mList.addAll(list)
                    mAdapter.notifyDataSetChanged()
                    mBinding.mRecyclerView.handlerPosition(page)
                }
            })
        }
    }




}